package com.zzvcom.webapp.action.admin;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zzvcom.persist.model.user.User;
import com.zzvcom.service.UserManager;
import com.zzvcom.utils.ExtMessage;
import com.zzvcom.utils.ServiceUtil;

@Controller
@Scope("prototype")
public class UserLogAction
{

	private UserManager manager;

	private String oldpassword;

	private String password1;

	private String password2;

	private ExtMessage msg = new ExtMessage();// 返回信息

	public String execute()
	{
		Object userObj=ServiceUtil.getSessionValue("user");
		if(userObj==null)
		{
			msg.setSuccess(false);
			msg.setInfo("请重新登录！");
			return "msg";
		}
		else
		{
			User user=(User)userObj;
			if(!user.getPassword().equals(oldpassword))
			{
				msg.setSuccess(false);
				msg.setInfo("old password is error！");
				return "msg";
			}
			else
			{
				if(password1.equals(password2))
				{
					user.setPassword(password1);
					manager.save(user);
					msg.setInfo("密码修改成功！");
				}
				else
				{
					msg.setSuccess(false);
					msg.setInfo("Confirm password and new password must be the same!");
					return "msg";
				}
			}
		}		
		return "msg";
	}
	
	public String logout()
	{
		ServletActionContext.getRequest().getSession().invalidate();
		return "login";
	}

	public String getOldpassword()
	{
		return oldpassword;
	}

	public void setOldpassword(String oldpassword)
	{
		this.oldpassword = oldpassword;
	}

	public String getPassword1()
	{
		return password1;
	}

	public void setPassword1(String password1)
	{
		this.password1 = password1;
	}

	public String getPassword2()
	{
		return password2;
	}

	public void setPassword2(String password2)
	{
		this.password2 = password2;
	}

	public ExtMessage getMsg()
	{
		return msg;
	}

	public void setMsg(ExtMessage msg)
	{
		this.msg = msg;
	}

	public UserManager getManager()
	{
		return manager;
	}

	@Autowired
	public void setManager(UserManager manager)
	{
		this.manager = manager;
	}

}
